package com.db.gffxo.epi.publish;

import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.net.SyslogAppender;

public class Log4JSyslogPublisher extends AbstractPublisher{

	//The logger is the standard log4j logger for the class where as
	static Logger logger = Logger.getLogger(Publisher.class);
	// The syslogger is intended to send the data to remote syslog
	static Logger syslogger = Logger.getLogger("SYSLOG");
	
	
	
	public void configurePublisher () {
		//S.removeAllAppenders();
		try {
			logger.info("Creating the syslog publisher");
			
			
			syslogger.removeAllAppenders();			
			Layout la = new PatternLayout("%m");
			FileAppender fa = new FileAppender(la,"SYSLOGGER.txt");
			syslogger.addAppender(fa);
			SyslogAppender sa = new SyslogAppender(la,"localhost",SyslogAppender.LOG_USER);
			
			// TODO Will need to see how these should be set or if we format the header ourselves
			sa.setHeader(true);
			sa.setFacilityPrinting(false);
			
			syslogger.addAppender(sa);
			syslogger.setAdditivity(false);

			logger.info("Syslogger created");
			
			syslogger.info("Testing the syslogger 2: \n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void main (String[] args) {
		
		
		Log4JSyslogPublisher pub = new Log4JSyslogPublisher ();
		pub.configurePublisher();
		
	}

	public String publishEvent(String message) {
		// TODO 
		syslogger.info(message);
		return "Success";
	}
}
